package cn.com.infcn.monitor.task.impl;

import org.apache.mina.core.session.IoSession;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.infcn.monitor.bean.IFCMem;
import cn.com.infcn.monitor.bean.IFCResponseBean;
import cn.com.infcn.monitor.util.IFCBeanUtils;
import cn.com.infcn.monitor.util.IFCMinitorConstanst;

/**
 * 获取系统内存相关信息
 * 
 * @author jjs
 *
 */
public class SysMemTask extends BaseTask {

	private final static Logger logger = LoggerFactory.getLogger(SysMemTask.class);

	public SysMemTask(IoSession session, Object message) {
		super(session, message);
	}

	@Override
	public void execute() throws Exception {
		IFCResponseBean result = mem();
		sendResponse(result);
	}

	private IFCResponseBean mem() throws SigarException {
		IFCResponseBean result = new IFCResponseBean();

		try {
			Sigar sigar = new Sigar();
			Mem m = sigar.getMem();
			IFCMem mem = new IFCMem();
			IFCBeanUtils.copyProperties(m, mem);

			Swap swap = sigar.getSwap();
			mem.setSwapFree(swap.getFree());
			mem.setSwapPageIn(swap.getPageIn());
			mem.setSwapPageOut(swap.getPageOut());
			mem.setSwapTotal(swap.getTotal());
			mem.setSwapUsed(swap.getUsed());

			result.setResult(mem);
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_ERROR);
			result.setResponseMsg(e.getMessage());
		}
		return result;
	}

}
